package com.qezhhnjy.demo.hadoop.pool;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.hadoop.fs.FileSystem;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author zhaoyangfu - 2021/2/4 13:11
 */
@Slf4j
@Component
public class HadoopConn {

    @Resource
    private GenericObjectPool<FileSystem> pool;

    private static final ThreadLocal<FileSystem> local = new ThreadLocal<>();

    public FileSystem open() throws Exception {
        FileSystem fs = pool.borrowObject();
        local.set(fs);
        return fs;
    }

    public void close() {
        FileSystem fs = local.get();
        pool.returnObject(fs);
        local.remove();
    }
}
